package com.wifi.eshipewifi;

import java.lang.ref.WeakReference;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wifi.BaseApplication;
import com.wifi.IBackService.IBackService;
import com.wifi.imp.MessageListener;

public class TestActivity extends Activity implements View.OnClickListener,MessageListener {
    private static final String TAG = "MainActivity";

    private IBackService iBackService;
    private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iBackService = null;

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iBackService = IBackService.Stub.asInterface(service);
        }
    };

    private TextView mResultText;
    private EditText mEditText;
    private Button send;
    private Intent mServiceIntent;

    @Override
    public void MessageGetListener(String msg) {
        mResultText.setText(msg);
    }

    class MessageBackReciver extends BroadcastReceiver {
        private WeakReference<TextView> textView;

        public MessageBackReciver(TextView tv) {
            textView = new WeakReference<TextView>(tv);
        }

        @Override
        public void onReceive(Context context, Intent intent) {
//            String action = intent.getAction();
//            TextView tv = textView.get();
//            if (action.equals(ThreadService.HEART_BEAT_ACTION)) {
//                if (null != tv) {
//                    tv.setText("Get a heart heat");
//                }
//            } else {
//                String message = intent.getStringExtra("message");
//                tv.setText(message);
//            }
        };
    }

    private MessageBackReciver mReciver;
    private IntentFilter mIntentFilter;
    private LocalBroadcastManager mLocalBroadcastManager;
    BaseApplication application;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        application = (BaseApplication) getApplication();
        application.setMsgListener(this);

        mResultText = (TextView) findViewById(R.id.resule_text);
        mEditText = (EditText) findViewById(R.id.content_edit);
        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(this);

//        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
//        mReciver = new MessageBackReciver(mResultText);
//
//        mServiceIntent = new Intent(this, ThreadService.class);
//        mIntentFilter = new IntentFilter();
//        mIntentFilter.addAction(ThreadService.HEART_BEAT_ACTION);
//        mIntentFilter.addAction(ThreadService.MESSAGE_ACTION);

    }

    @Override
    protected void onStart() {
        super.onStart();
//        mLocalBroadcastManager.registerReceiver(mReciver, mIntentFilter);
//        bindService(mServiceIntent, conn, BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
//        unbindService(conn);
//        mLocalBroadcastManager.unregisterReceiver(mReciver);
//        application.setMsgListener(null);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send:
//                String content = "$init;";
//                try {
//
//
//
//                    boolean isSend = iBackService.sendMessage(content);//Send Content by socket
//                    Toast.makeText(this, isSend ? "success" : "fail",
//                            Toast.LENGTH_SHORT).show();
//                    mEditText.setText("");
//                } catch (RemoteException e) {
//                    e.printStackTrace();
//                }
//                application.sendMsg("$init;");
                Intent intent=new Intent(TestActivity.this,LoginActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }
}
